package com.orderplus.opshop.synces.service.product.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orderplus.opshop.common.util.GsonUtil;
import com.orderplus.opshop.common.util.JsonValidator;
import com.orderplus.opshop.common.util.MapUtil;
import com.orderplus.opshop.common.util.ValueUtil;
import com.orderplus.opshop.core.data.request.RequestParam;
import com.orderplus.opshop.core.data.response.ResponseData;
import com.orderplus.opshop.synces.bean.ProductDetailModel;
import com.orderplus.opshop.synces.constant.EsConstant;
import com.orderplus.opshop.synces.entity.product.ProductSku;
import com.orderplus.opshop.synces.mapper.product.ProductSkuMapper;
import com.orderplus.opshop.synces.service.elasticsearch.data.Condition;
import com.orderplus.opshop.synces.service.elasticsearch.data.QueryParamData;
import com.orderplus.opshop.synces.service.elasticsearch.data.QueryResultData;
import com.orderplus.opshop.synces.service.elasticsearch.data.Type;
import com.orderplus.opshop.synces.service.elasticsearch.impl.OpSearchService;
import com.orderplus.opshop.synces.service.product.ProductSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * remark
 *
 * @author mhj
 * @date 2019-04-16 18:45:30
 */
@Service("productSkuService")
public class ProductSkuServiceImpl extends ServiceImpl<ProductSkuMapper, ProductSku> implements ProductSkuService {
	private final String PROD_SKU_ID_PREFIX = "ProdSkuId_";

	@Autowired
	private OpSearchService opSearchService;

	@Override
	public List<ProductDetailModel> mapFromProductSkuToProductDetailModel(Map<String, Object> map,String action,List<ProductDetailModel> productDetailModelList) {
		try {
//			把表名的字段名中包含_替换
			Map<String, Object> newMap = MapUtil.assemble(map);

//			转成product表的数据
			ProductSku productSkuBean = (ProductSku) MapUtil.mapToObject(newMap, ProductSku.class);

			ProductDetailModel.ProductSku productSku = setData(productSkuBean,action);

			ProductDetailModel productDetailModel = new ProductDetailModel();
			productDetailModel.setProductId(ValueUtil.toStr(productSku.getProductId()));
			Map<String, ProductDetailModel.ProductSku> skyMap = new ConcurrentHashMap<>();
			String key = PROD_SKU_ID_PREFIX + productSku.getSkuId();

//			String queryKey = new StringBuffer("skuMap.").append(key).append(".skuId").toString();
//			List<Map<String, Object>> list = this.queryProductSkyList(productSku, key);
//			if (ValueUtil.isNotEmpty(list)) {
//				for (Map<String, Object> queryMap : list) {
//					if(queryMap.containsKey(queryKey)){
//						skyMap = new ConcurrentHashMap<>();
//						skyMap.put(key, productSku);
//					}
//				}
//			} else {
//				skyMap = new ConcurrentHashMap<>();
//			}

			skyMap.put(key, productSku);
			productDetailModel.setSkuMap(skyMap);
			productDetailModelList.add(productDetailModel);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return productDetailModelList;
	}

	private ProductDetailModel.ProductSku setData(ProductSku productSkuBean,String action){
		ProductDetailModel.ProductSku productSku = new ProductDetailModel.ProductSku();

		if (ValueUtil.isNotEmpty(productSkuBean.getId())) {
			productSku.setId(ValueUtil.toStr(productSkuBean.getId()));
		}
		if (ValueUtil.isNotEmpty(productSkuBean.getSkuId())) {
			productSku.setSkuId(ValueUtil.toStr(productSkuBean.getSkuId()));
		}
		if (ValueUtil.isNotEmpty(productSkuBean.getProductId())) {
			productSku.setProductId(ValueUtil.toStr(productSkuBean.getProductId()));
		}
		if (ValueUtil.isNotEmpty(productSkuBean.getAliSkuId())) {
			productSku.setAliSkuId(productSkuBean.getAliSkuId());
		}
		if (ValueUtil.isNotEmpty(productSkuBean.getWholesaleLimit())) {
			productSku.setLimit(productSkuBean.getWholesaleLimit());
		}
		if (ValueUtil.isNotEmpty(productSkuBean.getWholesaleCount())) {
			productSku.setCount(productSkuBean.getWholesaleCount());
		}
		JsonValidator jsonValidator = new JsonValidator();

		if (ValueUtil.isNotEmpty(productSkuBean.getPriceIn()) && jsonValidator.validJson(productSkuBean.getPriceIn())) {
			List<ProductDetailModel.ProductSku.SkuPrice> pricesList = GsonUtil.toList(productSkuBean.getPriceIn(), ProductDetailModel.ProductSku.SkuPrice.class);

//				Map<String, List<ProductDetailModel.ProductSku.SkuPrice>> pricesInMap = new ConcurrentHashMap<>();

//				pricesInMap.put(PROD_SKU_PRICE_ID_PREFIX + productSkuBean.getProductId(), pricesList);

			productSku.setPricesIn(pricesList);
		}

		if (ValueUtil.isNotEmpty(productSkuBean.getPriceJson()) && jsonValidator.validJson(productSkuBean.getPriceJson())) {
			List<ProductDetailModel.ProductSku.SkuPrice> pricesList = GsonUtil.toList(productSkuBean.getPriceJson(), ProductDetailModel.ProductSku.SkuPrice.class);

//				Map<String, List<ProductDetailModel.ProductSku.SkuPrice>> pricesInMap = new ConcurrentHashMap<>();

//				pricesInMap.put(PROD_SKU_PRICE_ID_PREFIX + productSkuBean.getProductId(), pricesList);

			productSku.setPricesJson(pricesList);
		}

		if (ValueUtil.isNotEmpty(productSkuBean.getPriceJsonCn()) && jsonValidator.validJson(productSkuBean.getPriceJsonCn())) {
			List<ProductDetailModel.ProductSku.SkuPrice> pricesList = GsonUtil.toList(productSkuBean.getPriceJsonCn(), ProductDetailModel.ProductSku.SkuPrice.class);

//				Map<String, List<ProductDetailModel.ProductSku.SkuPrice>> pricesInMap = new ConcurrentHashMap<>();

//				pricesInMap.put(PROD_SKU_PRICE_ID_PREFIX + productSkuBean.getProductId(), pricesList);

			productSku.setPricesOutCn(pricesList);
		}

		if (ValueUtil.isNotEmpty(productSkuBean.getPriceJsonTh()) && jsonValidator.validJson(productSkuBean.getPriceJsonTh())) {
			List<ProductDetailModel.ProductSku.SkuPrice> pricesList = GsonUtil.toList(productSkuBean.getPriceJsonTh(), ProductDetailModel.ProductSku.SkuPrice.class);

//				Map<String, List<ProductDetailModel.ProductSku.SkuPrice>> pricesInMap = new ConcurrentHashMap<>();

//				pricesInMap.put(PROD_SKU_PRICE_ID_PREFIX + productSkuBean.getProductId(), pricesList);

			productSku.setPricesOutTh(pricesList);
		}

		if (ValueUtil.isNotEmpty(productSkuBean.getPriceJsonId()) && jsonValidator.validJson(productSkuBean.getPriceJsonId())) {
			List<ProductDetailModel.ProductSku.SkuPrice> pricesList = GsonUtil.toList(productSkuBean.getPriceJsonId(), ProductDetailModel.ProductSku.SkuPrice.class);

//				Map<String, List<ProductDetailModel.ProductSku.SkuPrice>> pricesInMap = new ConcurrentHashMap<>();

//				pricesInMap.put(PROD_SKU_PRICE_ID_PREFIX + productSkuBean.getProductId(), pricesList);

			productSku.setPricesOutId(pricesList);
		}

		return productSku;
	}

	private List<Map<String, Object>> queryProductSkyList(ProductDetailModel.ProductSku productSku, String queryKey) {
		RequestParam<QueryParamData> request = new RequestParam<>();
		QueryParamData queryParamData = new QueryParamData();
		queryParamData.setIndices(EsConstant.ES_INDICES);

		Condition[] conditions = new Condition[1];
		Condition.CommonCondition commonCondition = new Condition.CommonCondition(queryKey, Type.FieldType.TERM, Type.BoolType.MUST, productSku.getSkuId());

		conditions[0] = commonCondition;

		queryParamData.setConditions(conditions);
		queryParamData.setPageIndex(1);
		queryParamData.setPageSize(10);
		request.setRequestBody(queryParamData);
		ResponseData<QueryResultData.ListQueryResultData> response = opSearchService.call(request);

		return response.getData().getResultDetailList();
	}

	@Override
	public IPage<Map<String, Object>> pageMap(Page<Map<String, Object>> page) {
		return baseMapper.pageMap(page);
	}

}